﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleInfrastructure.FrameWorks;
using SimpleInfrastructure.Serialize;


namespace SimpleInfrastructure.Message
{
    public class DefaultNotifier:INotifier
    {
        private readonly string channel_prefix = "notify_";
        public void Notify(string src, string dst, string message, NotifyType ntype, object detail = null)
        {
            NotifyInfo info = new NotifyInfo() { Dst = dst,Src = src,Message = message,NotifyType = ntype};
            if (detail != null)
            {
                ISerializer serializer = SerializerProvider.GetSerializer(detail);
                Type t= detail.GetType();
                info.DetailType = t.FullName;
                var serializedDetail = serializer.Serialize(detail);
                if (serializedDetail is string)
                {
                    info.DetailString = serializedDetail;
                }
                else
                {
                    info.DetailBytes = serializedDetail;
                }
            }
            StackExchangeRedisHelper.Publish(GetChannel(ntype), info);
        }

        private string GetChannel(NotifyType ntype)
        {
            return string.Format("{0}{1}", channel_prefix,ntype);
        }
    }
}
